package com.dhcc.sdc.base.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.base.dao.ContraindicationItemRuleMapper;
import com.dhcc.sdc.base.entity.ContraindicationItemRuleEntity;
import com.dhcc.sdc.base.service.IContraindicationItemRuleService;
import com.dhcc.sds.standard.dao.SdsStandDicMapper;
import com.dhcc.sds.standard.dao.SdsStandSourceFieldMapper;
import com.dhcc.sds.standard.dao.SdsStandSourceMapper;
import com.dhcc.sds.standard.entity.SdsStandDicEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceFieldEntity;
import com.dhcc.sds.standard.service.ISdsStandSourceFieldService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 禁忌症条目匹配规则表数据服务层接口
 *
 * @ClassName: ContraindicationItemRuleServiceImpl
 * @Description: TODO
 * @author zhangjin
 * @date 2022-09-07 09:44:17
 */
@Service("contraindicationItemRuleService")
public class ContraindicationItemRuleServiceImpl extends BaseServiceImpl<ContraindicationItemRuleMapper, ContraindicationItemRuleEntity> implements IContraindicationItemRuleService {
	@Autowired
	private ISdsStandSourceService standSourceService;

	@Autowired
	private ISdsStandSourceFieldService sdsStandSourceFieldService;

	@Autowired
	private ContraindicationItemRuleMapper itemRuleMapper;
	@Autowired
	private SdsStandSourceMapper sourceMapper;
	@Autowired
	private SdsStandSourceFieldMapper sourceFieldMapper;
	@Autowired
	private SdsStandDicMapper sdsStandDicMapper;

	@Override
	public List<ContraindicationItemRuleEntity> page(Page<ContraindicationItemRuleEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<ContraindicationItemRuleEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}


	//获取条目关联规则
	@Override
	public List<ContraindicationItemRuleEntity> selectByExpression(Map<String, Object> map){

		List<ContraindicationItemRuleEntity> res = new ArrayList<>();
		String compare = (String) map.get("compare");
		if (Objects.equals("all", compare)){
			res = this.itemRuleMapper.selectList(null);
		}else {
			String ruleExpressions = (String) map.get("ruleExpressions");
			if (!StringUtils.isEmpty(ruleExpressions)){
				List<String> list = handleRuleExpressions(ruleExpressions);
				res = this.itemRuleMapper.selectByExpressions(list);
			}
		}

		for (ContraindicationItemRuleEntity ruleEntity:res){
			String ruleCal = "";
			//数据源1、取值项目1
			if (!StringUtils.isEmpty(ruleEntity.getDataSource1()) && !StringUtils.isEmpty(ruleEntity.getDataSourceItem1())){
				//数据源定义
				SdsStandSourceEntity sourceEntity1 = standSourceService.selectById(ruleEntity.getDataSource1());
				//数据源输出内容
				SdsStandSourceFieldEntity sourceFieldEntity1 = sdsStandSourceFieldService.selectById(ruleEntity.getDataSourceItem1());

				ruleCal = sourceEntity1.getSourceDesc() + ":" + sourceFieldEntity1.getFieldDesc();
			}
			//传参1
			if (!StringUtils.isEmpty(ruleEntity.getParam1())){
				ruleCal = ruleCal + "(" + ruleEntity.getParam1() + ")";
			}
			//运算1
			if (!StringUtils.isEmpty(ruleEntity.getOperator1())){
				ruleCal = ruleCal + ruleEntity.getOperator1();
			}

			//数据源2、 取值项目2
			if (!StringUtils.isEmpty(ruleEntity.getDataSource2()) && !StringUtils.isEmpty(ruleEntity.getDataSourceItem2())){
				SdsStandSourceEntity sourceEntity2 = standSourceService.selectById(ruleEntity.getDataSource2());
				SdsStandSourceFieldEntity sourceFieldEntity2 = sdsStandSourceFieldService.selectById(ruleEntity.getDataSourceItem2());
				ruleCal = ruleCal + sourceEntity2.getSourceDesc() + ":" + sourceFieldEntity2.getFieldDesc();
			}
			//传参2
			if (!StringUtils.isEmpty(ruleEntity.getParam2())){
				ruleCal = ruleCal + "(" + ruleEntity.getParam2() + ")";
			}
			//运算2
			if (!StringUtils.isEmpty(ruleEntity.getOperator2())){
				ruleCal = ruleCal + ruleEntity.getOperator2();
			}
			// ruleCal的值   电子病历:既往史(颅内出血)==true
			ruleEntity.setRuleCal(ruleCal);
		};
		return res;
	}

	@Override
	public JSONArray getSourceFieldMap(Long sourceId, Long linkStandSubId) {
		JSONArray array = new JSONArray();

		SdsStandSourceEntity sourceEntityData = new SdsStandSourceEntity();
		SdsStandSourceEntity sourceEntity = sourceMapper.selectById(sourceId);
		if (!StringUtils.isEmpty(sourceEntity)){
			String sourceDesc = sourceEntity.getSourceDesc();
			if (Objects.equals("非抗菌药物", sourceDesc) || Objects.equals("抗菌药物", sourceDesc) || Objects.equals("处置操作", sourceDesc)){
				sourceEntityData = standSourceService.getSourceByDesc("患者医嘱信息");
			}else if (Objects.equals("检验生化", sourceDesc) && StringUtils.isEmpty(linkStandSubId)){
				sourceEntityData = standSourceService.getSourceByDesc("患者送检报告信息");
			}else if (Objects.equals("影像检查", sourceDesc) && StringUtils.isEmpty(linkStandSubId)){
				sourceEntityData = standSourceService.getSourceByDesc("检查医嘱+诊断结果信息");
			}else if (Objects.equals("检验生化", sourceDesc) && !StringUtils.isEmpty(linkStandSubId)){
				sourceEntityData = standSourceService.getSourceByDesc("检验结果信息");
			}else if (Objects.equals("影像检查", sourceDesc) && !StringUtils.isEmpty(linkStandSubId)){
				sourceEntityData = standSourceService.getSourceByDesc("检查项目结果信息");
			}else {
				sourceEntityData = sourceEntity;
			}

			Wrapper<SdsStandSourceFieldEntity> qw = new EntityWrapper<>();
			qw.eq("source_id", sourceEntityData.getId());
			List<SdsStandSourceFieldEntity> sdsStandSourceFieldEntities = sourceFieldMapper.selectList(qw);
			for (SdsStandSourceFieldEntity entity : sdsStandSourceFieldEntities) {
				JSONObject obj = new JSONObject();
				obj.put("value", entity.getId().toString());
				obj.put("text", entity.getFieldDesc());
				array.add(obj);
			}
		}
		return array;

	}

	@Override
	public JSONArray getDicMapBySourceId(Long sourceId) {
		List<SdsStandDicEntity> standDicEntitys = sdsStandDicMapper.selectList(new EntityWrapper<SdsStandDicEntity>()
				.eq("source_id", sourceId));
		JSONArray array = new JSONArray();
		for (SdsStandDicEntity entity : standDicEntitys) {
			JSONObject obj = new JSONObject();
			obj.put("value", entity.getId().toString());
			obj.put("text", entity.getName());
			array.add(obj);
		}
		return array;
	}

	@Override
	public List<String> selectCode(String expCode) {
		return itemRuleMapper.selectCode(expCode);
	}

	public JSONObject getJsonMap(ContraindicationItemRuleEntity contraindicationItemRule) {
		JSONObject ruleMap = new JSONObject();

		ruleMap.put("dataSource1",contraindicationItemRule.getDataSource1());
		ruleMap.put("dataSource2",String.valueOf(contraindicationItemRule.getDataSource2()));
		ruleMap.put("dataSourceItem1",String.valueOf(contraindicationItemRule.getDataSourceItem1()));
		ruleMap.put("dataSourceItem2",String.valueOf(contraindicationItemRule.getDataSourceItem2()));

		return ruleMap;
	}

	private List<String> handleRuleExpressions(String ruleExpressions) {
		//小括号及其里面内容
		String p1 = "\\(.*?\\)";
		List<String> res = new ArrayList<>();
		Pattern p = Pattern.compile(p1);
		Matcher m = p.matcher(ruleExpressions);
		while (m.find()){
			String ruleExpression = m.group().substring(1, m.group().length() - 1);
			res.add(ruleExpression);
		}
		return res;
	}
}
